<template>
  <div>
    <div>  <el-tree
      ref="tree"
      :data="TreeFormList"
      show-checkbox
      node-key="id"
      check-strictly
      :props="{label:'name'}"
    /></div>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">

        <el-button size="small" @click="cancelButton">取 消</el-button>
        <el-button size="small" type="primary" @click="setRolesBtn">确 定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionListAPI, AssignPermissions } from '@/api/PermissionAPI'
import { getRolesAPI } from '@/api/settingAPI'
import { TreeData } from '@/utils/shuxin'
export default {
  name: 'AssignPermission',
  props: {
    roleId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      TreeFormList: [],
      data: []
    }
  },
  created() {
    // 调用获取权限列表
    this.getPermissionList()
    // 调用当前用户详情
    this.getRoles()
  },
  methods: {
    // 取消按钮
    cancelButton() {
      this.$emit('close')
    },

    // 设置权限按钮
    async setRolesBtn() {
      console.log(this.$refs.tree.getCheckedKeys())
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await AssignPermissions({ id: this.roleId, permIds: permIds })

      this.$message.success(res.message)
      this.$emit('uplie')
    },
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermissionListAPI(this.roleId)

      this.TreeFormList = TreeData(res.data, '0')
    },
    // 获取当前用户详情
    async getRoles() {
      const res = await getRolesAPI(this.roleId)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    }
  }

}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>

